<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 
        w3c                           函数名
        btn.addEventListener('click',回调函数,false)
        btn.addEventListener('click',function(){},false)

        ie9以上的浏览器才支持 

        ie8
        btn.attachEvent('onclick',function(){},false)

        btn.onclick=function(){}

     -->
    <button>增加事件</button>
    <script>
        var btn = document.querySelector('button');

        btn.addEventListener('click', function () {
            console.log('单击');
        });

        // 了解即可
        //    btn.attachEvent('onclick',function(){
        //        console.log('ie8兼容');
        //    })
        btn.onclick = function () {
            console.log('单击');
        }

        // 兼容性的封装
        function addEventListener(element, eventName, fn) {
            if (element.addEventListener) {
                element.addEventListener(eventName, fn)
            } else if (element.attachEvent) {
                element.attachEvent('on' + eventName, fn)

            } else {
                element['on' + eventName] = fn;
            }
        }


        // 使用
        addEventListener(btn, 'click', function () {
            console.log('用封装的函数进行单击');
        })
    </script>
</body>

</html>